

function id(id) {
    return document.getElementById(id);
}

function bg() {
    return chrome.extension.getBackgroundPage();
}

function getFilterString() {
	if(id("url_filter").value == "" || id("url_filter").value == "--type filter text--") {
		bg().filter_str = null;		
		return null;
	}
	else {
		bg().filter_str = id("url_filter").value;
		return id("url_filter").value;
	}
}

function buildMyAccordion(search_str) {
    var div;
    var outer_div = document.createElement("div");
		 
    if (bg().requests.length == 0) {
        
        div = document.createElement("div");
        div.innerHTML = "---No requests available---<br/>Issue HTTP requests to forge these.";
        outer_div.appendChild(div);
    }
    else {
        var found = false;
        var mehtod_str;
        var param_str;
        var p_arr;
        var font;
		  var i;
		  var end;
		  if(bg().ordering_latest) {
		  		// display latest requests first in accordion
		  		i = bg().requests.length - 1;
		  		end = -1;
		  }else {
		  		i = 0;
		  		end = bg().requests.length;		  
		  }
		  
		  
        while (i != end) {
            if (search_str !== null && search_str != undefined && search_str !== "") {
                if(search_str == "POST" || search_str == "GET") {
                    if (bg().requests[i].method.toUpperCase() != search_str) {
								if(bg().ordering_latest) {
									i--;				
								}else {
									i++;	
								}                        
                        continue;
                    }
                }
                else if (bg().requests[i].url.toString().indexOf(search_str) == -1) {
							if(bg().ordering_latest) {
								i--;				
							}else {
								i++;	
							}                    
                    	continue;
                }
            }
            found = true;

            div = document.createElement("div");
            method_str = bg().requests[i].method.toUpperCase();
            if (method_str == "POST") {
                method_str = '<font color="#B80000">' + method_str + "</font>";
            }
            else {
                method_str = '<font color="#0000FF">' + method_str + "</font>";
            }
            var html_str = '<font color="#009933">' + i + '</font>&nbsp;' + method_str;  + ": " + bg().requests[i].url;
            if (bg().requests[i].session_warning) {
                html_str += ": <b><font color='red'>! </font></b>";
            }
            else {
                html_str += ": ";
            }
            html_str += bg().requests[i].url;
            div.innerHTML = html_str;
            div.setAttribute("style", "width:100%;white-space:nowrap;background-color:#E6E6E6;border-width:1px;border-style:solid;border-color:#A4A4A4;");
            div.onmouseover = mouseover_div;
            div.onmouseout = mouseout_div;
            div.onclick = getOpenDivOnClick(i);
            outer_div.appendChild(div);
            div = document.createElement("div");
            div.id = "open_div_" + i;
            div.setAttribute("style", "display:none;"); // ueber onclick auf display:block setzen
            outer_div.appendChild(div);
				
				if(bg().ordering_latest) {
					i--;				
				}else {
					i++;	
				}
        } // Accordion filled with results
        
        if (!found) {
            div = document.createElement("div");
            div.innerHTML = "Searching: " + search_str + "<br/>---No matches found---";
            outer_div.appendChild(div);
        }
    }
    id("accordion").appendChild(outer_div);
}


function mouseover_details() {
    this.style.cursor = "pointer";
    this.style.backgroundColor = "#A9F5D0";
}

function mouseout_details() {
    this.style.cursor = "auto";
    this.style.backgroundColor = "";
}

function mouseover_div() {
    this.style.cursor = "pointer";
    this.style.backgroundColor = "#F6D8CE";
}

function mouseout_div() {
    this.style.cursor = "auto";
    this.style.backgroundColor = "#E6E6E6";
}

function getOpenDivOnClick(i) {
    handler = function () {
        var div = id("open_div_" + i);
        if (div.style.display == "none") {
            // aufklappen
            var param_str = "";
            var p_arr = bg().requests[i].params;
            if (p_arr.length > 0) {
                for (j = 0; j < p_arr.length; j++) {
							
                     param_str += ("<b>" + p_arr[j].name + "=</b>" + p_arr[j].value + "<br/>");
                }
            }
            else {
                param_str += "<i>No Parameters-</i>";
            }
            param_str = param_str.substr(0, param_str.length - 1);
            div.innerHTML = "&nbsp;&nbsp;&rarr;&nbsp;<i>edit parameters:</i>&nbsp;&nbsp;<br/>" + param_str;
            div.onmouseover = mouseover_details;
            div.onmouseout = mouseout_details;
            div.onclick = getHandler(i);
            div.style.display = "block";
        }
        else {
            // zuklappen
            div.innerHTML = "";
            div.style.display = "none";
        }
    }
    return handler;
}

function getHandler(i) {
    // handler muss in separater Function erstellt werden, wird der hanlder iteriert erzeugt wird er immer selber ueberschrieben
    handler = function () {
        //alert(JSON.stringify(bg().requests[i]));
        var url_str = chrome.extension.getURL("ForgePanel.html") + "?r=" + encodeURIComponent(JSON.stringify(i));
        chrome.tabs.create({ url: url_str });
    };
    return handler;
}

function setIcon() {
    if (bg().capture_mode > -1) {
        if (bg().blocking_mode) {
            if (bg().warning_found) {
                chrome.browserAction.setIcon({ path: "IconRedWarning.ico" });
            }
            else {
                chrome.browserAction.setIcon({ path: "IconRed.ico" });
            }
        }
        else {
            if (bg().warning_found) {
                chrome.browserAction.setIcon({ path: "IconGreenWarning.ico" });
            }
            else {
                chrome.browserAction.setIcon({ path: "IconGreen.ico" });
            }
        }
    }
    else {
        if (bg().warning_found) {
            chrome.browserAction.setIcon({ path: "IconWarning.ico" });
        }
        else {
            chrome.browserAction.setIcon({ path: "Icon.ico" });
        }
    }
}

id("blocking_mode_button").onclick = function () {
    if (this.checked == true) {
        bg().blocking_mode = true;
        setIcon();
    }
    else {
        bg().blocking_mode = false;
        setIcon();
    }
}

id("radio0").onclick = function () {
    bg().capture_mode = 0; // capture all
    setIcon();
};
id("radio1").onclick = function () {
    bg().capture_mode = 1; // capture only form active tab
    setIcon();
};
id("radio2").onclick = function () {
    bg().capture_mode = -1; // capture nothing
    setIcon();
};

if (bg().capture_mode == 0) {
    id("radio0").checked = "checked";
} else if (bg().capture_mode == 1) {
    id("radio1").checked = "checked";
} else if (bg().capture_mode == -1) {
    id("radio2").checked = "checked";
}

id("radio_order_1").onclick = function() {
	bg().ordering_latest = 1;
	id("accordion").innerHTML = "";
   buildMyAccordion(getFilterString());
};
id("radio_order_0").onclick = function() {
	bg().ordering_latest = 0;
	id("accordion").innerHTML = "";
   buildMyAccordion(getFilterString());
};


if(bg().ordering_latest == 1) {
	id("radio_order_1").checked = "checked";
}else {
	id("radio_order_0").checked = "checked";
}


id("url_filter").onkeyup = function () {
	filter_str = jQuery.trim(id("url_filter").value);
   id("accordion").innerHTML = "";
   buildMyAccordion(getFilterString());
}

id("url_filter").onfocus = function() {
	if(this.value == "--type filter text--") {
		this.value = "";	
	}
};

id("url_filter").onblur = function() {
	if(this.value == "") {
		this.value = "--type filter text--";	
	}
};


id("reset_button").onclick = function () {
    bg().requests = [];
    bg().sessions = {};
    bg().warning_found = false;
    id("accordion").innerHTML = "";
    buildMyAccordion(null);
    setIcon();
};

id("dump_all_button").onclick = function () {
    open_url_str = "Dump.html?r=ALL";
    var newWin = window.open(open_url_str, "SessionID Menu", "height=800, width=1024,toolbar=no,scrollbars=yes,menubar=no,location=no");
};

id("load_dump_button").onclick = function () {
    open_url_str = "Dump.html?r=LOAD";
    var newWin = window.open(open_url_str, "SessionID Menu", "height=800, width=1024,toolbar=no,scrollbars=yes,menubar=no,location=no");
}


if (bg().filter_str !== null) {
    buildMyAccordion(bg().filter_str);
    id("url_filter").value = bg().filter_str;
    if(bg().filter_str == "") {
    	id("url_filter").value = "--type filter text--";	
    }
}
else {
    buildMyAccordion(null);
    id("url_filter").value = "--type filter text--";
}

setIcon();

if (bg().blocking_mode) {
    id("blocking_mode_button").checked = "checked";
}
else {
    id("blocking_mode_button").checked = "";
}


$(document).ready(function () {
    //$("#accordion").accordion();
    $("#radios_capturing").buttonset();
    $("#radios_ordering").buttonset();
    $("#reset_button").button();
    $("#filter_button").button();
    //$("#add_sessionid_name").button();
    $("#dump_all_button").button();
    $("#load_dump_button").button();
    $("#blocking_mode_button").button();
});